import tkinter as tk
from tkinter import messagebox, ttk
import requests
import json
from login_window import LoginWindow
from register_window import RegisterWindow
from consultation_window import ConsultationWindow
from admin_window import AdminWindow

class MainApp:
    def __init__(self):
        self.root = tk.Tk()
        self.root.title("智能购车咨询系统")
        self.root.geometry("800x600")
        self.root.configure(bg='#f0f0f0')
        
        self.current_user = None
        self.api_base = "http://localhost:5000/api"
        
        self.create_welcome_screen()
    
    def create_welcome_screen(self):
        """创建欢迎界面"""
        for widget in self.root.winfo_children():
            widget.destroy()
        
        # 主标题
        title_label = tk.Label(
            self.root, 
            text="🚗 智能购车咨询系统", 
            font=("Arial", 24, "bold"),
            bg='#f0f0f0',
            fg='#2c3e50'
        )
        title_label.pack(pady=40)
        
        # 副标题
        subtitle_label = tk.Label(
            self.root,
            text="基于大语言模型的个性化购车建议平台",
            font=("Arial", 14),
            bg='#f0f0f0',
            fg='#7f8c8d'
        )
        subtitle_label.pack(pady=10)
        
        # 功能说明
        features_frame = tk.Frame(self.root, bg='#f0f0f0')
        features_frame.pack(pady=30)
        
        features = [
            "🤖 AI智能推荐 - 基于大语言模型的精准车型推荐",
            "💰 预算规划 - 根据预算推荐最适合的车型",
            "📊 专业分析 - 详细的车况、价格、维护成本分析",
            "📱 使用场景匹配 - 针对不同使用场景的个性化建议",
            "🏆 积分奖励 - 完成咨询获得积分兑换礼品"
        ]
        
        for feature in features:
            feature_label = tk.Label(
                features_frame,
                text=feature,
                font=("Arial", 12),
                bg='#f0f0f0',
                fg='#34495e',
                justify='left'
            )
            feature_label.pack(anchor='w', pady=5)
        
        # 按钮框架
        button_frame = tk.Frame(self.root, bg='#f0f0f0')
        button_frame.pack(pady=40)
        
        # 用户登录按钮
        user_login_btn = tk.Button(
            button_frame,
            text="用户登录",
            command=self.show_user_login,
            font=("Arial", 12),
            bg='#3498db',
            fg='white',
            width=15,
            height=2
        )
        user_login_btn.grid(row=0, column=0, padx=10)
        
        # 用户注册按钮
        user_register_btn = tk.Button(
            button_frame,
            text="用户注册",
            command=self.show_user_register,
            font=("Arial", 12),
            bg='#2ecc71',
            fg='white',
            width=15,
            height=2
        )
        user_register_btn.grid(row=0, column=1, padx=10)
        
        # 管理员登录按钮
        admin_login_btn = tk.Button(
            button_frame,
            text="管理员登录",
            command=self.show_admin_login,
            font=("Arial", 12),
            bg='#e74c3c',
            fg='white',
            width=15,
            height=2
        )
        admin_login_btn.grid(row=0, column=2, padx=10)
    
    def show_user_login(self):
        """显示用户登录窗口"""
        LoginWindow(self.root, self, "user")
    
    def show_user_register(self):
        """显示用户注册窗口"""
        RegisterWindow(self.root, self)
    
    def show_admin_login(self):
        """显示管理员登录窗口"""
        LoginWindow(self.root, self, "admin")
    
    def user_login_success(self, user_data):
        """用户登录成功回调"""
        self.current_user = user_data
        self.show_consultation_window()
    
    def admin_login_success(self, admin_data):
        """管理员登录成功回调"""
        self.show_admin_window()
    
    def show_consultation_window(self):
        """显示咨询主窗口"""
        ConsultationWindow(self.root, self)
    
    def show_admin_window(self):
        """显示管理员窗口"""
        AdminWindow(self.root, self)
    
    def logout(self):
        """退出登录"""
        self.current_user = None
        self.create_welcome_screen()
    
    def run(self):
        """运行应用"""
        self.root.mainloop()

if __name__ == "__main__":
    app = MainApp()
    app.run()